


import bpy
import mathutils

from bpy.types import Operator

import 全局控制.Node.节点和插座基类.bl_节点基类 as 基类
import 全局控制.Node.插座.bl_插座基类 as 插座基类

import math


E_计算类型枚举 = [
	("TYPE_勾股定理_取斜边长", "取斜边长", "", 0),
	("TYPE_勾股定理_取直角边长", "取直角边长", "", 1),
]



def f_代码头置入_勾股定理(self, context):
	代码头 = '# A:AValue，B:BValue = C:data  --  a**2 + b**2 = c**2'
	self.m_Code.lines[0].body = 代码头

class C_Node_勾股定理(基类.C_SN_基类):
	bl_dname = 'bl_node_勾股定理'
	bl_label = '勾股定理'
	
	m_运算类型 = bpy.props.EnumProperty(items = E_计算类型枚举, name="边长", default="TYPE_勾股定理_取斜边长" )
	m_Code = bpy.props.PointerProperty(type=bpy.types.Text, update=f_代码头置入_勾股定理)
	def init(self, context):
		super(C_Node_勾股定理, self).f_init(context)
		self.inputs.new('C_NodeSocket_f', name='A')
		self.inputs.new('C_NodeSocket_f', name='B')
		self.outputs.new('C_NodeSocket_f', name='C')
		
	def draw_buttons(self, context, layout):
		if self.m_Code == None :
			layout.prop(self, 'm_运算类型')
		layout.prop(self, 'm_Code')

	def f_update(self, 是否向下传递=False, input插座=None):
		AValue = 插座基类.f_取列表单值(self.inputs['A'].f_getData())
		BValue = 插座基类.f_取列表单值(self.inputs['B'].f_getData())
		#print('self.inputs[].f_getData() = ', type(self.inputs['B'].f_getData()))
		#print(' f_update self.m_运算类型 == ', AValue, BValue)
		if self.m_Code == None :
			if self.m_运算类型 == 'TYPE_勾股定理_取斜边长' :
				c = math.sqrt(AValue**2 + BValue**2)
			elif self.m_运算类型 == 'TYPE_勾股定理_取直角边长' :
				c = math.sqrt(AValue**2 - BValue**2)
			
			self.outputs['C'].f_setData(c)

		else:
			全局变量 = {'AValue':AValue, 'BValue':BValue, 'data':0.0}
			代码 = self.m_Code.as_string() + '\n'
			exec(代码, 全局变量)
			#print('全局变量[data] = ', self.name, 全局变量['data'])
			self.outputs['C'].f_setData(全局变量['data'])
		
		super(C_Node_勾股定理, self).f_update(是否向下传递, input插座)
		

	def f_input插座Update(self, input插座, 上层Output=None):
		if input插座 :
			v = input插座.f_getData()
			input插座.f_setData(v)
		super(C_Node_勾股定理, self).f_input插座Update(input插座, 上层Output)

class C_NODE_AddMenu_勾股定理(bpy.types.Operator):
	bl_idname = 'node.global_node_pythagorean'
	bl_label = '勾股定理'
	bl_options = {'REGISTER', 'UNDO'}
	
	def execute(operator, context):
		operator.use_transform = True
		bpy.ops.node.add_node(type='C_Node_勾股定理', use_transform=True)
		return {'FINISHED'}

	












E_向量属性枚举 = [
	("TYPE_向量_length", "长度", "", 0),
	("TYPE_向量_length_squared", "长度平方", "", 1),
]

def f_代码头置入_向量(self, context):
	代码头 = '# A:AValue，B:BValue = C:data  --  a**2 + b**2 = c**2'
	self.m_Code.lines[0].body = 代码头

class C_Node_mathutils_向量(基类.C_SN_基类):
	bl_dname = 'bl_node_向量长度'
	bl_label = '向量长度'
	
	m_属性类型 = bpy.props.EnumProperty(items = E_向量属性枚举, name="成员", default="TYPE_向量_length" )
	m_Code = bpy.props.PointerProperty(type=bpy.types.Text, update=f_代码头置入_向量)
	def init(self, context):
		super(C_Node_mathutils_向量, self).f_init(context)
		self.inputs.new('C_Socket1D_Vec3', name='向量')
		self.inputs.new('C_Socket1D_Vec3', name='比较向量')
		self.outputs.new('C_NodeSocket_f', name='成员')

	def draw_buttons(self, context, layout):
		if self.m_Code == None :
			layout.prop(self, 'm_属性类型')
		layout.prop(self, 'm_Code')

	def f_update(self, 是否向下传递=False, input插座=None):
		向量 = self.inputs['向量'].f_getData()
		比较向量 = self.inputs['比较向量'].f_getData()

		if self.m_Code != None :
			pass
		else:
			data = []
			for v in 向量:
				vec = mathutils.Vector(tuple(v))

				if self.m_属性类型 == 'TYPE_向量_length' :
					data.append(vec.length)
				elif self.m_属性类型 == 'TYPE_向量_length_squared' :
					data.append(vec.length_squared)

			self.outputs['成员'].f_setData(data)

		super(C_Node_mathutils_向量, self).f_update(是否向下传递, input插座)


class C_NODE_AddMenu_mathutils_向量(bpy.types.Operator):
	bl_idname = 'node.global_node_mathutils_vector'
	bl_label = 'mathutils.Vector'
	bl_options = {'REGISTER', 'UNDO'}
	
	def execute(operator, context):
		operator.use_transform = True
		bpy.ops.node.add_node(type='C_Node_mathutils_向量', use_transform=True)
		return {'FINISHED'}








classes = (
	C_Node_勾股定理,
	C_NODE_AddMenu_勾股定理,

	C_Node_mathutils_向量,
	C_NODE_AddMenu_mathutils_向量,
	
)

def register():
	for c in classes:
		bpy.utils.register_class(c)
		
def unregister():
	for c in classes:
		bpy.utils.unregister_class(c)

	

